/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.carparkweb.actionbeans;

import cz.muni.fi.pa165.carpark.EmployeePosition;
import cz.muni.fi.pa165.carpark.TO.BusinessTripTO;
import cz.muni.fi.pa165.carpark.TO.EmployeeTO;
import cz.muni.fi.pa165.carpark.TO.RoleTO;
import cz.muni.fi.pa165.carpark.TO.ServiceCheckTO;
import cz.muni.fi.pa165.carpark.TO.UserTO;
import cz.muni.fi.pa165.carpark.TO.VehicleTO;
import cz.muni.fi.pa165.carpark.VehicleStatus;
import cz.muni.fi.pa165.carpark.servicies.AuthorizationService;
import cz.muni.fi.pa165.carpark.servicies.BusinessTripService;
import cz.muni.fi.pa165.carpark.servicies.EmployeeService;
import cz.muni.fi.pa165.carpark.servicies.ServiceCheckService;
import cz.muni.fi.pa165.carpark.servicies.VehicleService;
import java.util.Date;
import java.util.LinkedList;
import java.util.Random;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.integration.spring.SpringBean;

/**
 *
 * @author Martin
 */
@UrlBinding("/data")
public class DataActionBean implements ActionBean {

    private ActionBeanContext context;
    @SpringBean
    private VehicleService vehicleService;
    @SpringBean
    private EmployeeService employeeService;
    @SpringBean
    private ServiceCheckService serviceCheckService;
    @SpringBean
    private BusinessTripService businessTripService;
    
    @SpringBean
    private AuthorizationService authorizationService;

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    public Resolution fillData() {
        VehicleTO v1 = new VehicleTO();
        v1.setCarProducer("Mazda");
        v1.setCarModel("3");
        v1.setAvailableForPositions(EmployeePosition.MANAGER);
        v1.setAvailableForPositions(EmployeePosition.SALES_ASSOCIATE);
        v1.setEngineType("2.0 TDI");
        v1.setNumberOfKms(120000);
        v1.setStateLicencePlate(getPlate());
        v1.setVin("ABCDABCDABCDABCD1");
        v1.setYearOfManufacture(2010);
        v1.setStatus(VehicleStatus.AVAILABLE);

        VehicleTO v2 = new VehicleTO();
        v2.setCarProducer("Ford");
        v2.setCarModel("Fiest");
        v2.setAvailableForPositions(EmployeePosition.MANAGER);
        v2.setAvailableForPositions(EmployeePosition.SALES_ASSOCIATE);
        v2.setAvailableForPositions(EmployeePosition.ADMINISTRATIVE_ASSISTANT);
        v2.setEngineType("1.6 TSI");
        v2.setNumberOfKms(80000);
        v2.setStateLicencePlate(getPlate());
        v2.setVin("ABCDABCDABCDAOOO1");
        v2.setYearOfManufacture(2010);
        v2.setStatus(VehicleStatus.AVAILABLE);


        VehicleTO v3 = new VehicleTO();
        v3.setCarProducer("Skoda");
        v3.setCarModel("SuperB");
        v3.setAvailableForPositions(EmployeePosition.MANAGER);
        v3.setEngineType("2.0 TDI");
        v3.setNumberOfKms(0);
        v3.setStateLicencePlate(getPlate());
        v3.setVin("ABCDABCXXXCDABCD1");
        v3.setYearOfManufacture(2010);
        v3.setStatus(VehicleStatus.AVAILABLE);

        EmployeeTO e1 = new EmployeeTO();
        e1.setName("Martin");
        e1.setPosition(EmployeePosition.SALES_ASSOCIATE);
        EmployeeTO e2 = new EmployeeTO();
        e2.setName("Pepa");
        e2.setPosition(EmployeePosition.MANAGER);
        EmployeeTO e3 = new EmployeeTO();
        e3.setName("Frante");
        e3.setPosition(EmployeePosition.ADMINISTRATIVE_ASSISTANT);

        ServiceCheckTO sc1 = new ServiceCheckTO();
        sc1.setCheckPurpose("Motor");
        sc1.setCheckInterval(20000);

        ServiceCheckTO sc2 = new ServiceCheckTO();
        sc2.setCheckPurpose("Maintenance done");
        sc2.setCheckInterval(995959);

        ServiceCheckTO sc3 = new ServiceCheckTO();
        sc3.setCheckPurpose("expected to be done");
        sc3.setCheckInterval(555555);
        

        BusinessTripTO bt1 = new BusinessTripTO();
        bt1.setEmployee(e1);
        bt1.setVehicle(v2);
        bt1.setDateOfTrip(new Date());

        this.employeeService.createEmp(e1);
        this.employeeService.createEmp(e2);
        this.employeeService.createEmp(e3);
 
        this.serviceCheckService.createCheck(sc3);
        this.serviceCheckService.createCheck(sc2);
        this.serviceCheckService.createCheck(sc1);
        
        v3.addControlServiceCheck(sc1);
        v3.addControlServiceCheck(sc2);
        v3.addControlServiceCheck(sc3);
        
        this.vehicleService.create(v3);
        this.vehicleService.create(v2);
        this.vehicleService.create(v1);

        this.businessTripService.create(bt1);
        
        //this.serviceCheckService.create(sc4);
        
        RoleTO manager = new RoleTO();
        manager.setAuthority("manager");
        
        UserTO admin = new UserTO();
        admin.setPassword("admin");
        admin.setUserName("admin");
        admin.setRoles(new LinkedList<RoleTO>());
        admin.getRoles().add(manager);
        
        authorizationService.create(admin);

        return new RedirectResolution("/");
    }
    
    public String getPlate(){
        final String alphabet = "0123456789ABCDEFGHIJLKMNOPQRSTUVWXYZ";
        final int N = alphabet.length();

        Random r = new Random();
        
        StringBuilder sb = new StringBuilder();
        
        for (int i = 0; i < 7; i++) {
            sb.append(alphabet.charAt(r.nextInt(N)));
        }
        return sb.toString();
    }

    public VehicleService getVehicleService() {
        return vehicleService;
    }

    public void setVehicleService(VehicleService vehicleService) {
        this.vehicleService = vehicleService;
    }

    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    public ServiceCheckService getServiceCheckService() {
        return serviceCheckService;
    }

    public void setServiceCheckService(ServiceCheckService serviceCheckService) {
        this.serviceCheckService = serviceCheckService;
    }

    public BusinessTripService getBusinessTripService() {
        return businessTripService;
    }

    public void setBusinessTripService(BusinessTripService businessTripService) {
        this.businessTripService = businessTripService;
    }
}
